package edu.geo.filters;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.geo.containers.Json;
import edu.geo.dao.impl.GeometricDomainDao;
import edu.geo.db.conn.DBConnection;
import edu.geo.db.conn.DBConnectionProvider;
import edu.geo.service.IGeometricDomainServiceFacade;
import edu.geo.service.impl.GeometricDomainServiceFacade;

public class GeoProcessFactory {

	private static final String GEODB = "geodb";
	
	private static final Log LOGGER = LogFactory.getLog(GeometricDomainDao.class);
	
	private String type;
	private String name;
	private FeatureProcess addProcessDecorator;
	
	private DBConnectionProvider dbConnectionProvider = new DBConnection(GEODB);
	
	private IGeometricDomainServiceFacade geomDomainService = 
			new GeometricDomainServiceFacade(new GeometricDomainDao(dbConnectionProvider));

	public GeoProcessFactory(DBConnectionProvider dbConnectionProvider) {
		this.dbConnectionProvider = dbConnectionProvider;
		addProcessDecorator = new FeatureProcess(geomDomainService);
	}

	public void setRequest(Json request) {
		type = request.getType();
		name = request.getName();
	}

	public GeoProcess getGeoProcess() {
		if (type == null) {
			return new GeoProcessDecorator();
		} else if ("Feature".equalsIgnoreCase(type)) {
			return addProcessDecorator;
		} else if ("Operation".equalsIgnoreCase(type)) {
			if ("addFeature".equalsIgnoreCase(name)) {
				LOGGER.info("agregar elemento");
			    return addProcessDecorator;
			}
			else if ("deleteFeature".equalsIgnoreCase(name)) {
				LOGGER.info("borrar elemento");
			    return new DeleteFeature(geomDomainService);
			}
			else
				if ("deleteGeometricDomain".equalsIgnoreCase(name)) {
				LOGGER.info("Delete geometric-domain request");
				return new DeleteFeature(geomDomainService);
			}else if ("retrieveGridDomain".equalsIgnoreCase(name)) {
				LOGGER.info("GridgeomDomainProcess");
				return new GridGeomDomainProcessor(geomDomainService);
			}else if ("retrieveSpaghettiFeatures".equalsIgnoreCase(name)) {
				LOGGER.info("GridgeomDomainProcess");
				return new SpaghettiProcess(geomDomainService);
			}else if ("greenYaoAdjustment".equalsIgnoreCase(name)) {
				LOGGER.info("GridgeomDomainProcess");
				return new GreenYaoProcess(geomDomainService);
			}
		}		
		return new GeoProcessDecorator();
	}
}
